<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="BOM产品代码">
              <a-input placeholder="请输入物料代码" v-model="queryParam.materialCode"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="BOM产品名称">
              <a-input v-model="queryParam.materialName" />
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="物料代码">
              <a-input placeholder="请输入物料代码" v-model="queryParam.detailMaterialCode"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="物料名称">
              <a-input v-model="queryParam.detailMaterialName" />
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button icon="search" type="primary" @click="searchQuery">查询</a-button>
      <a-button icon="reload" type="primary" @click="searchReset">重置</a-button>
      <a-button icon="plus" type="primary" @click="handleAdd">新增</a-button>
      <a-button
        icon="plus"
        type="primary"
        @click="handleCopy"
        v-show="selectedMainId"
        style="background-color: #4CAF50;color: white;border-color:#4CAF50"
        >复制
      </a-button>
      <a-button icon="download" type="primary" @click="handleExportXlsTemplate('BOM单导入模板')">引出导入模板</a-button>
      <a-upload
        :action="importExcelUrl"
        :headers="tokenHeader"
        :multiple="false"
        :showUploadList="false"
        name="file"
        @change="handleImportExcel"
      >
        <a-button icon="import" type="primary">导入</a-button>
      </a-upload>

      <a-upload
        :action="importExcelUrlByJD"
        :headers="tokenHeader"
        :multiple="false"
        :showUploadList="false"
        name="file"
        @change="handleImportExcel"
      >
        <a-button
          style="background: #FF8F00;border: #FF8F00;"
          icon="import"
          type="primary"
          @click="uploadModel.openUpload = true"
          >金蝶导入
        </a-button>
      </a-upload>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择
        <a style="font-weight: 600">{{ selectedRowKeys.length }}</a
        >项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        class="j-table-force-nowrap"
        :scroll="{ x: true }"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange, type: 'radio' }"
        :customRow="clickThenSelect"
        @change="handleTableChange"
      >
        <template slot="status" slot-scope="text">
          <a-tag :color="text === '未审核' ? 'red' : 'green'">{{ text }}</a-tag>
        </template>

        <template slot="action" slot-scope="text, record">
          <div v-if="record.status === 1">
            <a @click="handleUndoCheck(record)">反审核</a>
          </div>
          <div v-else>
            <a @click="handleEdit(record)">编辑</a>
            <a-divider style="background-color: black" type="vertical" />
            <a @click="handleDelete(record.id)">删除</a>
            <a-divider style="background-color: black" type="vertical" />
            <a @click="handleCheck(record)">审核</a>
          </div>
        </template>
      </a-table>
    </div>

    <a-tabs defaultActiveKey="1">
      <a-tab-pane tab="BOM单物料" key="1">
        <HfBomDetailList :mainId="selectedMainId" ref="HfBomDetailList"></HfBomDetailList>
      </a-tab-pane>
    </a-tabs>

    <hfBom-modal ref="modalForm" @ok="modalFormOk"></hfBom-modal>
  </a-card>
</template>

<script>
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import HfBomModal from './modules/HfBomModal'
import { getAction } from '@/api/manage'
import '@/assets/less/TableExpand.less'
import HfBomDetailList from '@views/material/HfBomDetailList.vue'
import { getCurrentTime } from '@/utils/util'

export default {
  name: 'HfBomList',
  mixins: [JeecgListMixin],
  components: {
    HfBomDetailList,
    HfBomModal
  },
  data() {
    return {
      description: 'bom单信息管理页面',
      uploadModel: {
        openUpload: false,
        uploadLoading: false
      },
      queryParam: {},
      // 表头
      columns: [
        {
          title: 'BOM编号',
          align: 'center',
          dataIndex: 'bomCode'
        },

        {
          title: '工序',
          align: 'center',
          dataIndex: 'step_dictText'
        },
        {
          title: '物料代码',
          align: 'center',
          dataIndex: 'materialCode'
        },
        {
          title: '物料名称',
          align: 'center',
          dataIndex: 'materialName'
        },
        {
          title: '规格型号',
          align: 'center',
          dataIndex: 'sap_dictText'
        },
        {
          title: '物料属性',
          align: 'center',
          dataIndex: 'materialProperty_dictText'
        },
        {
          title: '单位',
          align: 'center',
          dataIndex: 'unit_dictText'
        },
        {
          title: '标准用量',
          align: 'center',
          dataIndex: 'number'
        },
        {
          title: '标准消耗工时',
          align: 'center',
          dataIndex: 'manHour'
        },
        {
          title: '收料仓库',
          align: 'center',
          dataIndex: 'warehouse_dictText'
        },
        {
          title: '审核状态',
          align: 'center',
          dataIndex: 'status_dictText',
          scopedSlots: { customRender: 'status' }
        },
        {
          title: '制单',
          align: 'center',
          dataIndex: 'operator'
        },
        {
          title: '审核员',
          align: 'center',
          dataIndex: 'checker'
        },
        {
          title: '审核时间',
          align: 'center',
          dataIndex: 'checkTime'
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 147,
          scopedSlots: { customRender: 'action' }
        }
      ],
      url: {
        list: '/material/hfBom/list',
        delete: '/material/hfBom/delete',
        deleteBatch: '/material/hfBom/deleteBatch',
        exportXlsUrl: '/material/hfBom/exportXls',
        exportXlsTemplateUrl: '/material/hfBom/exportXlsTemplate',
        importExcelUrl: 'material/hfBom/importExcel',
        importExcelUrlByJD: 'material/hfBom/importExcelByJD',
        check: '/material/hfBom/check',
        undoCheck: '/material/hfBom/undoCheck'
      },
      dictOptions: {},
      selectedMainId: '',
      superFieldList: [],
      selectedMaterialId: ''
    }
  },
  computed: {
    importExcelUrl: function() {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    },
    importExcelUrlByJD: function() {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrlByJD}`
    }
  },
  methods: {
    getCurrentTime,
    initDictConfig() {},
    clickThenSelect(record) {
      return {
        on: {
          click: () => {
            this.onSelectChange(record.id.split(','), [record])
          }
        }
      }
    },
    onClearSelected() {
      this.selectedRowKeys = []
      this.selectionRows = []
      this.selectedMainId = ''
    },
    onSelectChange(selectedRowKeys, selectionRows) {
      console.log('selectedRowKeys', selectedRowKeys)
      this.selectedMainId = selectedRowKeys[0]
      this.selectedRowKeys = selectedRowKeys
      this.selectionRows = selectionRows
      this.selectedMaterialId = selectionRows[0].materialId
    },
    loadData(arg) {
      if (!this.url.list) {
        this.$message.error('请设置url.list属性!')
        return
      }
      //加载数据 若传入参数1则加载第一页的内容
      if (arg === 1) {
        this.ipagination.current = 1
      }
      this.onClearSelected()
      var params = this.getQueryParams() //查询条件
      params['status'] = 0
      this.loading = true
      getAction(this.url.list, params).then(res => {
        if (res.success) {
          this.dataSource = res.result.records
          this.ipagination.total = res.result.total
        }
        if (res.code === 510) {
          this.$message.warning(res.message)
        }
        this.loading = false
      })
    },
    handleCopy() {
      let record = {}
      record = this.selectionRows[0]
      this.$refs.modalForm.copyAdd(record)
      this.$refs.modalForm.title = '复制新增'
      this.$refs.modalForm.disableSubmit = false
    }
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>
